package model;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import view.View;

//jatek osztaly
public class Game {
	public Map currentMap;			//az adott palya
	public ArrayList<GameObject> gameObjects;		//gameobjektumok gyujtemenye
	public GameObjectFactory gameobjectfactory;	//factory a game objectokra
	public MapFactory mapfactory;					// factory a mapra es a mapelementekre
	public AntKillerSpray killspray;				//hangyairto spray
	public AntOdorKillerSpray killodor;			//szagirto spray
	public Timing time;
	public View view;
    public boolean gameover=false;
	//konstruktor
	public Game(View viw){
		currentMap=new Map();
		gameobjectfactory=new GameObjectFactory(this);
		mapfactory=new MapFactory();
		killspray=new AntKillerSpray(this);
		killodor=new AntOdorKillerSpray(this);
		gameObjects=new ArrayList<GameObject>();
		time=new Timing(this);
		view=viw;

			HedgeHog h = (HedgeHog) gameobjectfactory.createGameObject("Hedgehog");
			h.loc_x=50;
			h.loc_y=40;
			gameObjects.add(h);
		
	}
	
	//betolti a palyat a megadott fajlbol
	public void loadMap(String mapname) throws IOException{
		BufferedReader br;
		br = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(mapname))));
	
    try {
    	//első sor beolvasása
    	String sor=br.readLine();
    	
    	// ha a beolvasott sor nem nulla, akkor lépünk a ciklusba és végezzük el a műveleteinket
    	while(sor!=null){
    		 //beolvasott sor tördelése szóköz alapján
 	    	 String[] result = sor.split(" ");
 	    	 //hangyaboly letrehozasa es a palyahoz hozzadasa
 	    	 if(result[0].equals("anthill")){
 	    		 currentMap.AddElem(mapfactory.createMapElement(result));
 	    		 
 	    	 }
 	    	//hangyaleso letrehozasa es a palyahoz hozzadasa
 	    	 if(result[0].equals("antlion")){
 	    		 currentMap.AddElem(mapfactory.createMapElement(result));
 	    		 
 	    	 }
 	    	//kaja letrehozasa es a palyahoz hozzadasa
 	    	 if(result[0].equals("food")){
 	    		 currentMap.AddElem(mapfactory.createMapElement(result));
 	    		 
 	    	 }
 	    	//szikla letrehozasa es a palyahoz hozzadasa
 	    	 if(result[0].equals("rock")){
 	    		 currentMap.AddElem(mapfactory.createMapElement(result));
 	    		 
 	    	 }
 	    	//to letrehozasa es a palyahoz hozzadasa
 	    	 if(result[0].equals("lake")){
 	    		 currentMap.AddElem(mapfactory.createMapElement(result));
 	    		 
 	    	 }
    		 sor=br.readLine();// új sor beolvasása a cikluban maradáshoz
    	 }
    	
    } catch (IOException ex) {
          ;
        }
	br.close();	//fajl bezarasa
      
		
	}

	public void isGameOver() {
		boolean flag=true;
		Food f;
		for (MapElement me:currentMap.mapelements){
			if (me.getType().toLowerCase().contains("food")){
				f=(Food)me;
				if (f.capacity>0)
					flag=false;
				
			}
		}
		if(flag)gameover=true;
	}
	


			
	}


